package com.example.myapplication1.ui.order;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication1.R;
import com.example.myapplication1.db.DatabaseHelper.FeedingRecord;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class FeedingRecordAdapter extends RecyclerView.Adapter<FeedingRecordAdapter.FeedingRecordViewHolder> {
    private List<FeedingRecord> feedingRecords = new ArrayList<>();
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());

    @NonNull
    @Override
    public FeedingRecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_feeding_record, parent, false);
        return new FeedingRecordViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FeedingRecordViewHolder holder, int position) {
        FeedingRecord record = feedingRecords.get(position);
        holder.bind(record);
    }

    @Override
    public int getItemCount() {
        return feedingRecords.size();
    }

    public void setFeedingRecords(List<FeedingRecord> feedingRecords) {
        this.feedingRecords = feedingRecords;
        notifyDataSetChanged();
    }

    static class FeedingRecordViewHolder extends RecyclerView.ViewHolder {
        private final TextView catNameText;
        private final TextView locationText;
        private final TextView feedTimeText;
        private final TextView feedDetailsText;
        private final SimpleDateFormat dateFormat;

        public FeedingRecordViewHolder(@NonNull View itemView) {
            super(itemView);
            catNameText = itemView.findViewById(R.id.cat_name);
            locationText = itemView.findViewById(R.id.location);
            feedTimeText = itemView.findViewById(R.id.feed_time);
            feedDetailsText = itemView.findViewById(R.id.feed_details);
            dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        }

        public void bind(FeedingRecord record) {
            // 只显示区域
            catNameText.setText(record.getCatRegion());
            // 隐藏位置信息
            locationText.setVisibility(View.GONE);
            feedTimeText.setText(dateFormat.format(record.getFeedTime()));
            // 设置喂养详情
            String feedDetails = record.getFeedType() + " x" + record.getFeedAmount();
            feedDetailsText.setText(feedDetails);
        }
    }
} 